package com.xiaoke.liantongdemo.ui;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.UUID;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

import com.hanvon.core.StrokeView;
import com.qoppa.samples.viewer.PDFViewer;
import com.xiaoke.liantongdemo.R;
import com.xiaoke.liantongdemo.constants.Constants;
import com.xiaoke.liantongdemo.pdf.PDFUtil;

public class BusinessConfirmActivity extends Activity {
	private final String path = "/sdcard/mysign.jpg";
	private int businessId;
	private final String uuid = UUID.randomUUID().toString();
	private Button sign;
	private Button manageConfirm;
	private PDFViewer m_PDFViewer;
	private StrokeView strokeView;
	private LinearLayout sign_layout;
	private LinearLayout sign_view;
	private Button sign_ok;
	private Button sign_cancel;
	private Button sign_clear;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		businessId = getIntent().getIntExtra(Constants.PARAM_NAME_BUSINESS_ID, 0);
//		BusinessInputs businessInputs = (BusinessInputs) getIntent().getSerializableExtra(Constants.PARAM_NAME_BUSINESS_INPUTS);
//		System.out.println(businessInputs);
		setContentView(R.layout.business_confirm);
		loadView();
		m_PDFViewer = new PDFViewer(this);
		try {
			m_PDFViewer.loadDocument(new FileInputStream(Constants.DATA_PATH + Constants.CREATED_PDF_PATH + Constants.TEMP_FILE_NAME + Constants.PDF_SUFFIX));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	

	private void fillSignature(String signaturePath){
		try {
			PDFUtil.fillPDFSignature(signaturePath, Constants.DATA_PATH + Constants.CREATED_PDF_PATH + Constants.TEMP_FILE_NAME
					+ Constants.PDF_SUFFIX, Constants.DATA_PATH + Constants.CREATED_PDF_PATH + businessId + "_" + uuid
					+ Constants.PDF_SUFFIX);
			m_PDFViewer.loadDocument(new FileInputStream(Constants.DATA_PATH + Constants.CREATED_PDF_PATH + businessId + "_" + uuid + Constants.PDF_SUFFIX));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void loadView() {
		sign_clear = (Button) findViewById(R.id.sign_clear);
		sign = (Button) findViewById(R.id.sign_bt);
		manageConfirm = (Button) findViewById(R.id.confirm_manage_bt);
		sign_layout = (LinearLayout) findViewById(R.id.sign_layout);
		sign_view = (LinearLayout) findViewById(R.id.sign_layout);
		sign_ok = (Button) findViewById(R.id.sign_ok);
		sign_cancel = (Button) findViewById(R.id.sign_cancle);
		manageConfirm.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				AlertDialog ad = new AlertDialog.Builder(BusinessConfirmActivity.this).setTitle("恭喜你")
						.setMessage("业务已经办理成功！").setPositiveButton("确定", new DialogInterface.OnClickListener() {
							
							@Override
							public void onClick(DialogInterface dialog, int which) {
								setResult(Constants.RESULT_CODE_MANAGED);
								finish();
								
							}
						}).setCancelable(false).show();
				
			}
		});
		sign_ok.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					strokeView.saveBitmap(path);
					fillSignature(path);
					sign_layout.removeView(strokeView);
					sign_layout.setVisibility(View.GONE);
					manageConfirm.setVisibility(View.VISIBLE);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});
		sign_cancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				sign_view.removeView(strokeView);
				sign_layout.setVisibility(View.GONE);
			}
		});
		sign.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				strokeView = new StrokeView(BusinessConfirmActivity.this, 800,
						480);
				sign_view.addView(strokeView);
				sign_layout.setVisibility(View.VISIBLE);
			}
		});
		sign_clear.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				sign_view.removeView(strokeView);
				strokeView = new StrokeView(BusinessConfirmActivity.this, 800,
						480);
				sign_view.addView(strokeView);
			}
		});
	}
}
